#include "frozen-bubble.h"

FBFont *font_new (const char *file)
{
	FBFont *font;
	char filename[100] = { 0 };
	
	strcat (filename, FPATH);
	strcat (filename, "gfx/");
	strcat (filename, file);
	
	if (!(font = malloc (sizeof (FBFont))))
	{
		fprintf (logfile, "font: Unable to malloc FBFont for '%s'\n", file);
		exit (EXIT_FAILURE);
		return NULL;
	}
	
	if (!(font->surface = IMG_Load (filename)))
	{
		fprintf (logfile, "font: Unable to load font into surface for '%s'\n", file);
		free (font);
		exit (EXIT_FAILURE);
		return NULL;
	}
	
	font->num_chars = 0;
	SDL_LockSurface (font->surface);
	int char_x = -1;
	
	for (int x = 0; x < font->surface->w; x++)
	{
		int pixel = ((Uint32 *)font->surface->pixels)[x];
		bool is_now_font = pixel != 0;
		
		if (is_now_font && (char_x < 0))
		{
			char_x = x;
		}
		else if (!is_now_font && (char_x >= 0))
		{
			int chindex = font->num_chars++;
			
			fprintf (logfile, "font: Char %d at %d,%d\n", chindex, char_x, 1);
			
			font->chars[chindex].x = char_x;
			font->chars[chindex].y = 1;
			font->chars[chindex].w = x - char_x;
			font->chars[chindex].h = font->surface->h - 1;
			
			char_x = -1;
		}
	}
	
	SDL_UnlockSurface (font->surface);
	return font;
}

void font_free (FBFont *font)
{

}

void font_render (FBFont *font, SDL_Surface dest, int x, int y, const char *txt)
{

}

